﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using HYHY_Core.Enums;
using HYHY_Core.Tool.Tool;
using HYQY_App.Applet.Interface;
using HYQY_Core.Entities.Ware;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HYQY_App.Applet.Dtos.SubPackagesDto;

namespace HYQY_App.Applet.Service
{
    // <summary>
    /// 小程序Home首页
    /// </summary>
    [ApiDescriptionSettings(ApiGroupEnumsCode.AppletSubPackages, Name = "AppletSubPackages")]
    public class SubPackagesService : ISubPackagesService, IDynamicApiController, ITransient
    {
        /// <summary>
        /// sql
        /// </summary>
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SubPackagesService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 获取商品信息详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SelectWareInfoDetailOutput> SelectWareInfoDetail(SelectWareInfoDetailInput input)
        {
            // 去掉前后空格
            ToolHelper.TrimStrings(input);

            var ware = await _db.Queryable<WareBank>()
                .FirstAsync(it => it.WareCode == input.WareCode) ?? throw Oops.Bah("商品信息不存在=>【SelectWareInfoDetail】");

            var images = await _db.Queryable<Images>().Where(it => it.Code == ware.WareCode).ToListAsync();


            var output = new SelectWareInfoDetailOutput
            {
                WareBank = ware,
                MainImageList = images.Where(it => it.Type == "Main").Select(it => it.Url).ToList(),
                DetailImageList = images.Where(it => it.Type == "Detail").Select(it => it.Url).ToList(),
                Message = "请求成功",
                StatusCode = 200,
            };
            return output;
        }
    }
}
